﻿using System;
using System.Collections.Generic;
using System.Text;
using Dapper_Core.Model;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace Dapper_Core.Common
{
    public static class SessionRedisUtility
    {
        private static RedisUtility redisUtility = new RedisUtility();

        /// <summary>
        /// 获取用户基本信息
        /// </summary>
        /// <returns></returns>
        public static BasicUserInfo GetBasicUserInfo()
        {
            try
            {
                string key = SessionKey.GetMainKey();
                if (!redisUtility.KeyExists(key)) return null;
                BasicUserInfo userInfo = new BasicUserInfo();
                userInfo.Email = redisUtility.HashGet<string>(key, "Email");
                userInfo.FirstDeptCode = redisUtility.HashGet<string>(key, "FirstDeptCode");
                userInfo.FirstDeptName = redisUtility.HashGet<string>(key, "FirstDeptName");
                userInfo.PinyinCode = redisUtility.HashGet<string>(key, "PinyinCode");
                userInfo.RemoteIpAddress = redisUtility.HashGet<string>(key, "RemoteIpAddress");
                userInfo.SecondDeptCode = redisUtility.HashGet<string>(key, "SecondDeptCode");
                userInfo.SecondDeptName = redisUtility.HashGet<string>(key, "SecondDeptName");
                userInfo.ThirdDeptCode = redisUtility.HashGet<string>(key, "ThirdDeptCode");
                userInfo.ThirdDeptName = redisUtility.HashGet<string>(key, "ThirdDeptName");
                userInfo.UserCode = redisUtility.HashGet<string>(key, "UserCode");
                userInfo.UserId = redisUtility.HashGet<string>(key, "UserId");
                userInfo.UserName = redisUtility.HashGet<string>(key, "UserName");
                return userInfo;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 设置用户基本信息
        /// </summary>
        /// <param name="userInfo"></param>
        public static void SetBasicUserInfo(BasicUserInfo userInfo)
        {
            string key = SessionKey.GetMainKey();
            var propertyInfoArray = userInfo.GetType().GetProperties();
            foreach (var propertyInfo in propertyInfoArray)
            {
                redisUtility.HashSet(key, propertyInfo.Name, JsonConvert.SerializeObject(propertyInfo.GetValue(userInfo, null)));
            }
            #region 手动添加各个属性
            //redisUtility.HashSet(key, "Email", JsonConvert.SerializeObject(userInfo.Email));
            //redisUtility.HashSet(key, "FirstDeptCode", JsonConvert.SerializeObject(userInfo.FirstDeptCode));
            //redisUtility.HashSet(key, "FirstDeptName", JsonConvert.SerializeObject(userInfo.FirstDeptName));
            //redisUtility.HashSet(key, "PinyinCode", JsonConvert.SerializeObject(userInfo.PinyinCode));
            //redisUtility.HashSet(key, "RemoteIpAddress", JsonConvert.SerializeObject(userInfo.RemoteIpAddress));
            //redisUtility.HashSet(key, "SecondDeptCode", JsonConvert.SerializeObject(userInfo.SecondDeptCode));
            //redisUtility.HashSet(key, "SecondDeptName", JsonConvert.SerializeObject(userInfo.SecondDeptName));
            //redisUtility.HashSet(key, "ThirdDeptCode", JsonConvert.SerializeObject(userInfo.ThirdDeptCode));
            //redisUtility.HashSet(key, "ThirdDeptName", JsonConvert.SerializeObject(userInfo.ThirdDeptName));
            //redisUtility.HashSet(key, "UserCode", JsonConvert.SerializeObject(userInfo.UserCode));
            //redisUtility.HashSet(key, "UserId", JsonConvert.SerializeObject(userInfo.UserId));
            //redisUtility.HashSet(key, "UserName", JsonConvert.SerializeObject(userInfo.UserName));
            #endregion
        }

        /// <summary>
        /// 获取Session的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">Session的键</param>
        /// <returns></returns>
        public static T GetSessionData<T>(string key)
        {
            try
            {
                if (redisUtility.StringGet(key) == null)
                {
                    return default(T);
                }
                return JsonConvert.DeserializeObject<T>(redisUtility.StringGet(key));
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }

        /// <summary>
        /// 设置Session的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">Session的键</param>
        /// <returns></returns>
        public static void SetSessionData<T>(string key, T value)
        {
            redisUtility.StringSet(key, JsonConvert.SerializeObject(value));
        }

        /// <summary>
        /// 清空Session
        /// </summary>
        public static void ClearSession()
        {
            redisUtility.KeyDelete(SessionKey.GetMainKey());
            redisUtility.KeyDelete(SessionKey.GetAllMenusCodeKey());
            redisUtility.KeyDelete(SessionKey.GetAllMenuPageUrlsKey());
        }
    }
}
